package com.mz.ieann.aide.widget.reward

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Path
import android.util.AttributeSet
import android.util.Log

/**
 * <p>------------------------------------------------------
 * <p> If there is technical discussion, you can contact zhangmz90@foxmail.com
 * <p>------------------------------------------------------
 * <p> 扇形区域角度大小有入参的概率值确定
 * <p>
 *
 * @author Created by zhangmz
 * @date on 2023/2/28
 */
class RewardRatioView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0
): BaseRewardView(context, attrs, defStyle) {

    override fun start() {
        if (probability.isNullOrEmpty()){
            Throwable("call addProbability must be set before starting")
            return
        }
        if (describe.isNullOrEmpty()){
            Throwable("call addDescribe must be set before starting")
            return
        }
        super.start()
    }

    /**
     * 点击开始的时候，去绘制转盘
     */
    override fun drawStart(canvas: Canvas) {
        super.drawStart(canvas)
        drawOutsideCircle(canvas)
        drawArc(canvas)
        drawInsidePoint(canvas)
        drawInsideCircle(canvas)
    }

    /**
     * 扇形区域按中将概率分配角度
     */
    override fun drawArc(canvas: Canvas) {
        val count = probability?.size?: 0.coerceAtMost(describe?.size?: 0)
        for (i in 0 until count) {
            val pro = probability?.get(i)?: 0F
            val sweepAngle = 360 * pro
            val startAngle = if (i == 0) {
                revolveAngle - sweepAngle / 2
            } else {
                nextStartAngle
            }

            // 根据比例大小绘制扇形角度
            lotteryPaint.color = if (i % 2 == 0) Color.RED else Color.BLUE
            canvas.drawArc(
                outRectF,
                startAngle,
                sweepAngle,
                true,
                lotteryPaint
            )
            nextStartAngle = startAngle + sweepAngle

            // 居中绘制文字，扇形位置
            val des = describe?.get(i)?: "UNKNOW"
            val path = Path()
            path.addArc(outRectF, startAngle, sweepAngle)
            canvas.drawTextOnPath(
                des,
                path,
                // 文字横向偏移量
                0f,
                // 文字竖向偏移量
                radius / 8,
                textPaint
            )
            path.close()
        }
    }

}